//
//  EngineerController.m
//  PianoAndPoemDirectSeeding
//
//  Created by 梅津山 on 2017/7/21.
//  Copyright © 2017年 梅津山. All rights reserved.
//

#import "EngineerController.h"
#import <AVOSCloud.h>
#import "HomeModel.h"
#import <MJRefresh/MJRefresh.h>
#import <MBProgressHUD/MBProgressHUD.h>
#import "EngineerCell.h"
#import "NowEngineerController.h"


@interface EngineerController ()

@property (nonatomic, strong) NSArray *dataArr;

@end

@implementation EngineerController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self dataCliced];
}

- (void)dataCliced{
    // [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AVQuery *query = [AVQuery queryWithClassName:@"Content"];
    [query orderByDescending:@"createdAt"];
    // owner 为 Pointer，指向 _User 表
    [query includeKey:@"owner"];
    // image 为 File
    [query includeKey:@"imageArr"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects) {
            //  [MBProgressHUD hideHUDForView:self.view animated:YES];
            NSMutableArray *nowArr = [NSMutableArray arrayWithCapacity:objects.count];
            for (AVObject *objc in objects) {
                [self.tableView.mj_header endRefreshing];
                
                NSArray *fileArr = [objc valueForKey:@"imageArr"];
                AVUser *user = [objc objectForKey:@"owner"];
                
                NSString *par = [objc objectForKey:@"particulars"];
                NSString * loca = [objc objectForKey:@"location"];
                NSString *state = [objc objectForKey:@"state"];
                NSString *grade = [objc objectForKey:@"grade"];
                NSDate *createdAt = [objc objectForKey:@"createdAt"];
                NSString *image =  [user objectForKey:@"image"];
                NSString *name = user.username;
                //  NSLog(@"%@",[objc objectForKey:@"particulars"]);
                NSMutableArray *urlArr = [NSMutableArray arrayWithCapacity:fileArr.count];
                for (AVFile *file in fileArr) {
                    [urlArr addObject:file.url];
                }
                
                NSMutableDictionary *dictAll = [NSMutableDictionary dictionary];
                if (user) {
                    [dictAll setObject:user forKey:@"owner"];
                }
                if (grade) {
                    [dictAll setObject:grade forKey:@"grade"];
                }
                if (state) {
                    [dictAll setObject:state forKey:@"state"];
                }
                if (par) {
                    [dictAll setObject:par forKey:@"particulars"];
                }
                if (loca) {
                    [dictAll setObject:loca forKey:@"location"];
                }
                if (createdAt) {
                    [dictAll setObject:createdAt forKey:@"createdAt"];
                }
                if (urlArr) {
                    [dictAll setObject:urlArr forKey:@"imageArr"];
                }
                if (image) {
                    [dictAll setObject:image forKey:@"image"];
                }
                if (name) {
                    [dictAll setObject:name forKey:@"name"];
                }
                HomeModel *model = [[HomeModel alloc] initWithNewDict:dictAll];
                [nowArr addObject:model];
            }
            //            NSArray *data = objects[@"results"];
            self.dataArr = nowArr;
            [self.tableView reloadData];
        }
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EngineerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EngineerCell" forIndexPath:indexPath];
    cell.news = self.dataArr[indexPath.row];
    return cell;
}

- (IBAction)LeftBtnClicked:(UIBarButtonItem *)sender {
    NowEngineerController *now = [NowEngineerController sharedNow];
    [now leftMynow];
}



@end
